<script>
export default {
  name: "Question",
  data() {
    return {
      questionList: [],
      currentPage: 1,
      size: 4,
      total: 0,
      key: '',
      tagList: [],
      currentName: '全部',
      currenTagId:0,
      paginationKey: 0,
      code:0,

    }
  },
  created() {
    this.getQuestionList()
    this.getQuestionAllTag()
  },
  methods: {
    getQuestionList() {
      this.request.get('/question/front/list', {
        params: {
          currentPage: this.currentPage,
          size: this.size,
          key: this.key
        }
      })
          .then(res => {
            this.code=res.code

            this.total = res.page.totalCount
            this.questionList = res.page.list
          })
          .catch(error => {
            console.error(error);
          });
    },
    handleSizeChange(val) {
      this.size = val
      this.currentPage = 1
      if ( this.currentName==='全部'){

        this.getQuestionList()
      }else {
        this.getQuestionByTag()
      }

    },
    // 当前页
    // 当前页
    handleCurrentChange(val) {
      this.currentPage = val; // 确保更新 currentPage 的值
      if (this.currentName === '全部') {
        this.getQuestionList();
      } else {
        this.getQuestionByTag(); // 现在会使用更新后的 currentPage 请求数据
      }


    },
    openDetailPage(id) {
      // 调用后端接口更新浏览次数
      this.request.get(`/question/front/updateBrowseNumber/${id}`)
          .then(res => {
            console.log('浏览次数更新成功');
            // 成功后再跳转到详情页
            this.$router.push(`/questionDetail/${id}`);
          })
          .catch(error => {
            console.error('浏览次数更新失败', error);
            // 即使更新失败也允许跳转（可根据实际需求调整）
            this.$router.push(`/questionDetail/${id}`);
          });
    },
    getQuestionAllTag() {
      this.request.get('/questionTag/front/findAll')
          .then(res => {
            this.tagList = res.questionTagList
            this.tagList.unshift({tagName: '全部'})

          })
    },
    selectType(tagName,id) {
      console.log(id)
      this.currentPage = 1; // 重置页数为第一页
      this.paginationKey += 1; // 增加 key 的值来强制重新渲染分页组件
      this.currenTagId=id;
      if (tagName==='全部'){
        this.getQuestionList()
      }else {
        this.getQuestionByTag()
      }
      this.currentName = tagName
    },
    getQuestionByTag(){
      this.request.get('/question//front/findQuestionByTag/'+`${this.currentPage}` + "/" + `${this.size}` +"/"+ this.currenTagId)
          .then(res=>{
            this.code=res.code
            this.total = res.count
            this.questionList=res.questionVoList
          })
    }
  }
}
</script>

<template>
  <div class="main animate__animated animate__fadeInDown">
    <div style="display: flex;grid-gap: 10px">
      <div class="card" style="width: 150px;height: 760px">
        <div class="type-item" :class="{'type-item-active' :item.tagName===currentName}"
             v-for="item in tagList " :key="item.id" @click="selectType(item.tagName,item.id)">{{ item.tagName }}
        </div>
      </div>
      <div class="card" style="height: 760px;width: 900px;align-items: center;margin-left: 10px">
        <div v-if="questionList.length > 0" v-for="(item,index) in questionList" :key="index" class="question-container"
             @click="openDetailPage(item.id)">
          <div class="question ">
            <img :src="item.questionHover" alt="">
            <div class="author-info">
              <img style="width: 18px; height: 20px; border-radius: 10px;margin-left: 115px" :src="item.avatar" alt="">
              <span style="margin-left: 1px">{{ item.nickName }}</span>
              <span style="margin-left: 20px"><i class="el-icon-date"></i> {{ item.createTime }}</span>
              <div class="question-type">{{ item.questionTag }}</div>
            </div>
            <div class="question-content">
              <h2>{{ item.questionTitle }}</h2>
              <p>{{ item.questionDescription }}...</p>
            </div>
            <div class="interactions">
              <div class="browse">
                <img style="width: 25px; height: 25px; background-color: transparent !important;"
                     src="@/assets/images/browse.png" alt="">
                <span>{{ item.browerNumber }}</span>
              </div>
              <div class="like">
                <img style="width: 25px; height: 25px; background-color: transparent !important;"
                     src="@/assets/images/like.png" alt="">
                <span style="margin-right: 5px">{{ item.likeNumber }}</span>
              </div>
              <div class="favorite">
                <img style="width: 25px; height: 25px; background-color: transparent;" src="@/assets/images/collect.png"
                     alt="">
                <span>{{ item.collectNumber }}</span>
              </div>
            </div>
          </div>
        </div>

        <div v-if="code===400" class="no-data">
          <div>
            <img src="../../assets/images/question.png">
          </div>
          <h1>还没有对应的试题哦,敬请等待哦!</h1>
        </div>

        <!-- 分页插件容器 -->
        <div class="pagination-container" v-if="code===200">
          <el-pagination
              big
              background
              :key="paginationKey"

              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              layout="prev, pager, next"
              :total="total"
              :current-page="currentPage"
              :page-size="size"
              class="mt-4"
          />
        </div>

      </div>

    </div>
  </div>

</template>

<style scoped>
.main {
  margin-top: 1px;
  margin-left: 225px;

}

.card {
  padding: 20px;
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 2px 5px 0 rgb(0, 0, 0, 0.1);
}

.type-item {
  text-align: center;
  padding: 10px 0;
  font-size: 16px;
  cursor: pointer;

}

.type-item-active {
  background-color: #52bcce;
  color: #FFFFFF;
  border-radius: 5px;
}

.question-container {
  display: flex;
  height: 170px;
}

.question {
  display: flex;
  align-items: center;
  width: 800px;
  margin: auto;
  background-color: #ffffff;
  border: 1px solid #938d8d;
  border-radius: 10px;
  padding: 25px;
  box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
  position: relative;
  transition: transform 0.3s ease, box-shadow 0.3s ease; /* 添加过渡效果 */

  cursor: pointer;
}

.question:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 15px rgba(0,0,0,0.2), 0 12px 22px rgba(0,0,0,0.2);
}
.single-line {
  white-space: nowrap; /* 确保文本在一行内显示 */
  overflow: hidden; /* 超出容器部分隐藏 */
  text-overflow: ellipsis; /* 超出部分显示省略号 */
  max-width: 90%; /* 根据需要调整最大宽度 */
  cursor: pointer;
}

/* 新增的红色字体样式 */
.red-color {
  color: red;
}

.question h2 {
  margin-top: -35px;

}

.question p {
  margin-top: 1px;
}

.question img {
  width: 100px;
  height: 100px;
  margin-right: 7px;
  border-radius: 10px;
}

.question h2 {
  font-size: 24px;
}

.question p {
  font-size: 16px;
}

.question-type {
  position: absolute;
  top: -7px;
  right: -205px;
  padding: 5px 10px;
  background-color: #2eb9b9;
  color: #fff;
  font-size: 14px;
  border-radius: 5px;
}


.author-info {
  margin-top: 100px;
  position: absolute;
  display: flex;
  align-items: center;
}

.interactions {
  position: absolute;
  right: 0;
  display: flex;
  align-items: center;
  margin-right: 10px;
  margin-top: 90px;
}

.interactions .like,
.interactions .browse,
.interactions .favorite {
  display: flex;
  align-items: center;
  margin-left: 5px;
}

.interactions img {
  width: 20px;
  height: 20px;
  margin-right: 5px;
}

.interactions span {
  font-size: 12px;
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.no-data {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 200px;
  font-size: 24px;
  color: #666;
}
.no-data img {
  width: 300px;
  height: 300px;
  margin-bottom: 20px;
  margin-top: 500px;
}

</style>